package commands;

import java.util.List;

import user.User;
import messages.*;
import core.UserManager;
import event.ListPlayersEventArgs;
import game.Player;


/**
 * The ListPlayers command.
 * 
 * This command will handle ListPlayers messages.
 */
public class ListPlayersCommand extends Command {

	
	public ListPlayersCommand(Message message, User user) {
		super(message, user);
	}
	
	
	@Override
	public void execute() {
		UserManager userManager = this.controller.getUserManager();
		ListPlayersMessage message = (ListPlayersMessage) this.message;
		
		synchronized(userManager) {
			List<Player> players = (message.all() ? userManager.getPlayers() : userManager.getOnlinePlayers());
			ListPlayersReplyMessage replyMsg = new ListPlayersReplyMessage(players);	
						
			// Fires the ListPlayers event.
			this.controller.fireListPlayersEvent(new ListPlayersEventArgs(this.user.getLogin(), replyMsg));
		}
	}
}
